package com.slicktechnologies.client.views.settings.user;

import com.google.gwt.user.client.ui.*;
import com.simplesoftwares.client.library.*;
import com.simplesoftwares.client.library.appskeleton.*;
import com.simplesoftwares.client.library.appstructure.formtablescreen.*;
import com.simplesoftwares.client.library.appstructure.search.MyQuerry;
import com.simplesoftwares.client.library.mywidgets.*;
import com.slicktechnologies.client.utility.*;
import com.simplesoftwares.client.library.appstructure.*;
import com.slicktechnologies.shared.common.businessunitlayer.*;
import com.slicktechnologies.shared.common.helperlayer.*;
import com.slicktechnologies.shared.common.personlayer.*;
import com.slicktechnologies.shared.common.productlayer.Category;

import com.google.gwt.event.dom.client.*;
/**
 * FormTablescreen template.
 */
public class UserForm extends FormTableScreen<User>  implements ClickHandler {

	//Token to add the varialble declarations
	ObjectListBox<Branch>lblBranch;
	CheckBox cbStatus;
	TextBox tbUserId;
	PasswordTextBox ptbPassword,ptbReTypePassword;
	ObjectListBox<Employee> lbEmployeeName;
	


	//Token to add the concrete presenter class name
	//protected <PresenterClassName> presenter;



	public UserForm  (SuperTable<User> table, int mode,
			boolean captionmode) {
		super(table, mode, captionmode);
		createGui();

	}

	private void initalizeWidget()
	{

		lblBranch=new ObjectListBox<Branch>();
		AppUtility.makeBranchListBoxLive(lblBranch);

	
		

		cbStatus=new CheckBox();
		cbStatus.setValue(true);  
		tbUserId=new TextBox();

		ptbPassword=new PasswordTextBox();

		ptbReTypePassword=new PasswordTextBox();

		lbEmployeeName=new ObjectListBox<Employee>();
		AppUtility.makeSalesPersonListBoxLive(lbEmployeeName);

		

	}

	/*
	 * Method template to create the formtable screen
	 */

	@Override
	public void createScreen() {

		//Token to initialize the processlevel menus.
		initalizeWidget();



		//////////////////////////////////Form Field Declaration/////////////////////////////////////////////////


		//////////////////////////// Form Field Initialization////////////////////////////////////////////////////

		//Token to initialize formfield
		FormFieldBuilder fbuilder;
		fbuilder = new FormFieldBuilder();
		FormField fgroupingUserInformation=fbuilder.setlabel("User Information").widgetType(FieldType.Grouping).setMandatory(false).setColSpan(5).build();
		fbuilder = new FormFieldBuilder("* Employee Name",lbEmployeeName);
		FormField flbEmployeeName= fbuilder.setMandatory(true).setMandatoryMsg("Employee Name is Mandatory").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Branch",lblBranch);
		FormField flblBranch= fbuilder.setMandatory(true).setMandatoryMsg("Branch is Mandatory").setRowSpan(0).setColSpan(0).build();

		fbuilder = new FormFieldBuilder("Status",cbStatus);
		FormField fcbStatus= fbuilder.setMandatory(false).setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* User Id",tbUserId);
		FormField ftbUserId= fbuilder.setMandatory(true).setMandatoryMsg("User Id is Mandatory").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Password",ptbPassword);
		FormField fptbPassword= fbuilder.setMandatory(true).setMandatoryMsg("Password is Mandatory").setRowSpan(0).setColSpan(0).build();
		fbuilder = new FormFieldBuilder("* Re Type Password",ptbReTypePassword);
		FormField fptbReTypePassword= fbuilder.setMandatory(true).setMandatoryMsg("Re Type Password is Mandatory").setRowSpan(0).setColSpan(0).build();


		

		/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

		////////////////////////////////Lay Out Making Code///////////////////////////////////////////////////////////////


		FormField[][] formfield = {   {fgroupingUserInformation},
				{flbEmployeeName,ftbUserId,fcbStatus},
			{fptbPassword,fptbReTypePassword}

		};


		this.fields=formfield;		
	}

	/**
	 * method template to update the model with token entity name
	 */
	@Override
	public void updateModel(User model) 
	{

		if(lbEmployeeName.getValue()!=null)
			model.setEmployeeName(lbEmployeeName.getValue());
		/*if(lblBranch.getValue()!=null)
  model.setBranch(lblBranch.getValue());*/
		if(cbStatus.getValue()!=null)
			model.setStatus(cbStatus.getValue());
		if(tbUserId.getValue()!=null)
			model.setUserName(tbUserId.getValue());
		if(ptbPassword.getValue()!=null)
			model.setPassword(ptbPassword.getValue());
		if(this.lbEmployeeName.getValue()!=null)
		{
			Employee employee=lbEmployeeName.getSelectedItem();
			String branch=employee.getBranchName();
			model.setBranch(branch);
		}
		

		presenter.setModel(model);


	}

	/**
	 * method template to update the view with token entity name
	 */
	@Override
	public void updateView(User view) 
	{


		if(view.getEmployeeName()!=null)
			lbEmployeeName.setValue(view.getEmployeeName());
		/*if(view.getBranch()!=null)
  lblBranch.setValue(view.getBranch());*/
		
		if(view.getStatus()!=null)
			cbStatus.setValue(view.getStatus());
		if(view.getUserName()!=null)
			tbUserId.setValue(view.getUserName());
		if(view.getPassword()!=null)
		{
			ptbPassword.setValue(view.getPassword());
			ptbReTypePassword.setValue(view.getPassword());

		}

		

		presenter.setModel(view);
	}

	/**
	 * Toggles the app header bar menus as per screen state
	 */

	public void toggleAppHeaderBarMenu() {
		if(AppMemory.getAppMemory().currentState==ScreeenState.NEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard"))
				{
					menus[k].setVisible(true); 
				}
				else
					menus[k].setVisible(false);  		   

			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.EDIT)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Save")||text.equals("Discard"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}

		else if(AppMemory.getAppMemory().currentState==ScreeenState.VIEW)
		{
			InlineLabel[] menus=AppMemory.getAppMemory().skeleton.getMenuLabels();
			for(int k=0;k<menus.length;k++)
			{
				String text=menus[k].getText();
				if(text.equals("Discard")||text.equals("Edit"))
					menus[k].setVisible(true); 

				else
					menus[k].setVisible(false);  		   
			}
		}
	
	}

	/**
	 * sets the id textbox with the passed count value. 
	 */
	@Override
	public void setCount(int count)
	{

	}


	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub

	}

	/**
	 * Extra validation which matches the typed pass word from retyped passwords
	 */
	@Override
	public boolean validate()
	{
		boolean result=super.validate();
		boolean presresult=true;

		String pass=this.ptbPassword.getText().trim();
		String retypepass=ptbReTypePassword.getText().trim();

		if((pass.equals("")==false&&retypepass.equals("")==false)&&pass.equals(retypepass)==false)
		{
			ptbReTypePassword.getElement().getStyle().setBorderColor("#dd4b39");
			//ptbPassword.getElement().getStyle().setBorderColor("#dd4b39");
			presresult=false;
		}
		if(presresult==false)
			showDialogMessage("Password Mismatch!");



		return result&&presresult;

	}


	@Override
	public void clear() {

		super.clear();
		cbStatus.setValue(true); 
	}





}
